package br.ufrj.ppgi.infovis.mtgraph.shapes;

import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.shapes.MTEllipse;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.components.visibleComponents.widgets.MTTextArea.ExpandDirection;
import org.mt4j.components.visibleComponents.widgets.keyboard.ITextInputListener;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.font.FontManager;
import org.mt4j.util.font.IFont;
import org.mt4j.util.math.Vector3D;

import br.ufrj.ppgi.infovis.mtgraph.GraphLineDragAction;
import br.ufrj.ppgi.infovis.util.TextureCache;

import processing.core.PApplet;

public class LabeledCircle extends MTEllipse implements ITextInputListener {
	
	private MTTextArea txta;
	private MTTextArea txta_shadow;
	int txtaInnerPadding = 10;
	private String label;
	private MTApplication pa;
	
	
	public LabeledCircle(PApplet pApplet, MTColor color, float radius, IFont font, String label) {
		super(pApplet,new Vector3D(250, 50, 10), radius, radius);
				
		this.setNoStroke(true);
		this.setFillColor(color);
		pa = (MTApplication) pApplet;
	
		this.addGestureListener(DragProcessor.class, new IGestureEventListener() {
			
			@Override
			public boolean processGestureEvent(MTGestureEvent arg0) {
				System.out.println("mudando de tamanho...");
				return false;
			}
		});
		
		
		this.setTexture(TextureCache.getTexture(TextureCache.TXT_ADD));
		
		this.txta = new MTTextArea(pApplet, 0, 0,radius * 2,radius * 2, font);
		txta.setInnerPadding(txtaInnerPadding);
		txta.setText(label);
		txta.setExpandDirection(ExpandDirection.UP);
		txta.setNoStroke(true);
		txta.setNoFill(true);
		txta.setPickable(false);
		txta.setEnableCaret(false);
		txta.setName("lblCircle");
		
		this.txta_shadow = new MTTextArea(pApplet, 0, 0, radius * 2 - 2, radius * 2 - 2, FontManager.getInstance().createFont(pApplet, "arial.ttf", 14, MTColor.BLACK));
		txta_shadow.setInnerPadding(txtaInnerPadding);
		txta_shadow.setText(label);
		txta_shadow.setExpandDirection(ExpandDirection.UP);
		txta_shadow.setNoStroke(true);
		txta_shadow.setNoFill(true);
		txta_shadow.setPickable(false);
		txta_shadow.setEnableCaret(false);
		txta_shadow.setName("lblCircle");
		
		this.addChild(txta);
		this.addChild(txta_shadow);
		
		txta.setPositionGlobal(this.getCenterPointLocal());
		txta_shadow.setPositionGlobal(this.getCenterPointLocal());
		txta_shadow.sendToFront();
		txta.sendToFront();
		
		setPositions();
	}
	
	public void setLabel(String text) {
		label = text;
		if (text == null || text.equals("")) return;
		this.txta.setText(text);
		this.txta_shadow.setText(text);
	}
	
	public String getLabel() {
		return label;
		//return this.txta.getText();
	}
	
	public void setColor(MTColor color) {
		this.setFillColor(color);
	}
	
	public String getText() {
		return label;
		//return this.txta.getText();
	}
	
	public int getX() {
		return (int)this.getCenterPointGlobal().x;
	}
	
	public int getY() {
		return (int)this.getCenterPointGlobal().y;
	}
	
	public MTTextArea getTextComponent() {
		return this.txta;
	}
	
	public void sendToFront() {
		super.sendToFront();
		txta_shadow.sendToFront();
		txta.sendToFront();
		//this.sendToFront(txta_shadow);
		//this.sendChildToFront(txta);
	}
	@Override
	public void drawComponent(processing.core.PGraphics g) {
		super.drawComponent(g);
		//this.xyz();
		//this.txta_shadow.setText(this.txta.getText());
	}
	
	public void refresh() {
		txta.drawComponent(pa.getPGraphics());
		txta_shadow.drawComponent(pa.getPGraphics());
	}
	

	@Override
	public void clear() {
		this.txta.clear();
		this.txta_shadow.clear();
		this.label = this.txta.getText();
	}

	@Override
	public void appendText(String text) {
		this.txta.appendText(text);
		this.txta_shadow.appendText(text);
		this.label = this.txta.getText();
	}

	@Override
	public void setText(String text) {
		this.txta.setText(text);
		this.txta_shadow.setText(text);
		this.label = this.txta.getText();
	}

	@Override
	public void appendCharByUnicode(String unicode) {
		this.txta.appendCharByUnicode(unicode);
		this.txta_shadow.appendCharByUnicode(unicode);
		this.label = this.txta.getText();
	}

	@Override
	public void removeLastCharacter() {
		this.txta.removeLastCharacter();
		this.txta_shadow.removeLastCharacter();
		this.label = this.txta.getText();
	}
	
	private void setPositions() {
		Vector3D pos;
		pos = this.getCenterPointGlobal();
		pos.setZ(1f);
		this.setPositionGlobal(pos);
		
		pos = txta_shadow.getCenterPointGlobal();
		pos.setZ(1f);
		txta_shadow.setPositionGlobal(pos);
		
		pos = txta.getCenterPointGlobal();
		pos.setZ(1f);
		txta.setPositionGlobal(pos);
		
		
		
	}
}
